home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / FILER / ECHO.ZIP / !Echo / c / echo
Text File  |  1996-01-19  |  8KB  |  344 lines

  1. /* echo.c */
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6.  
  7. #include "wimpt.h"
  8. #include "res.h"
  9. #include "msgs.h"
  10. #include "template.h"
  11. #include "baricon.h"
  12. #include "event.h"
  13. #include "dbox.h"
  14. #include "menu.h"
  15. #include "wimp.h"
  16. #include "os.h"
  17. #include "werr.h"
  18. #include "win.h"
  19. #include "tracker.h"
  20. #include "xfersend.h"
  21. #include "help.h"
  22.  
  23. #define echo__FOK     0           /* OK action button */
  24. #define echo__FName   2           /* name field */
  25. #define echo__FIcon   3           /* icon to drag. */
  26.  
  27. static char echo__srcname[256];
  28.  
  29. static int echo__filetype;
  30. static xfersend_saveproc  echo__saveproc;
  31. static xfersend_sendproc  echo__sendproc;
  32. static xfersend_printproc echo__printproc;
  33. static void *echo__savehandle;
  34. static int echo__estsize = 0;
  35. static char *echo__filename;
  36. static dbox echo__d;
  37.  
  38. static void echo__click(wimp_i i)
  39. {  i = i;
  40. }
  41.  
  42. static void echo__info(void)
  43. {  dbox inf;
  44.  
  45.    if (inf = dbox_new("progInfo"), !inf)
  46.       werr(FALSE, msgs_lookup("main2"));
  47.  
  48.    dbox_setfield(inf, 3, "1.00 (29 Apr 94)");
  49.    dbox_show(inf);
  50.    dbox_fillin(inf);
  51.    dbox_dispose(&inf);
  52. }
  53.  
  54. static void echo__menuhandler(void *handle, char *hit)
  55. {  handle = handle;
  56.  
  57.    switch (hit[0])
  58.    {  case 1:
  59.          if (hit[1]) echo__info();
  60.          break;
  61.  
  62.       case 2:
  63.          exit(0);
  64.          break;
  65.    }
  66. }
  67.  
  68. void echo__setname(wimp_w w, wimp_i ii, char *name)
  69. {
  70.    wimp_icreate i;
  71.    wimp_i iii;
  72.  
  73.    i.w = w;
  74.  
  75.    wimpt_noerr(wimp_get_icon_info(i.w, ii, &i.i));
  76.    wimpt_noerr(wimp_delete_icon(w, ii));
  77.  
  78.    i.i.flags &= ~wimp_ITEXT;        /* set not text */
  79.    i.i.flags |= wimp_ISPRITE + wimp_INDIRECT;       /* set sprite */
  80.    
  81.    strcpy(i.i.data.indirectsprite.name, name);
  82.  
  83.    if (wimp_spriteop(24, i.i.data.indirectsprite.name))
  84.       sprintf(i.i.data.indirectsprite.name, "application");
  85.  
  86.    i.i.data.indirectsprite.spritearea = (void *) 1;
  87.  
  88.    wimpt_noerr(wimp_create_icon(&i, &iii));
  89. }
  90.  
  91. static BOOL echo__saveevents(dbox d, void *ev, void *handle) 
  92. {
  93.    wimp_eventstr *e = (wimp_eventstr*) ev;
  94.  
  95.    if (help_dboxrawevents(d, ev, "SAVEAS")) return TRUE; /* handle interactive help */
  96.  
  97.    switch (e->e) 
  98.    {  case wimp_EBUT:
  99.          if (e->data.but.m.bbits == wimp_BDRAGLEFT)
  100.          {
  101.             dbox_getfield(d, echo__FName, echo__filename, 256);
  102.             xfersend(echo__filetype, echo__filename, echo__estsize,
  103.                      echo__saveproc, echo__sendproc,
  104.                      echo__printproc, e, handle);
  105.             return TRUE;
  106.          }
  107.    }
  108.  
  109.    return FALSE;
  110. }
  111.  
  112. BOOL echo__saveas(int filetype, char *name, int estsize,
  113.                   xfersend_saveproc  saveproc,
  114.                   xfersend_sendproc  sendproc,
  115.                   xfersend_printproc printproc,
  116.                   void *handle, char *spr)
  117. {  if (echo__d) return FALSE;
  118.    if (!echo__filename) echo__filename = malloc(256);
  119.    echo__d = dbox_new("xfer_send");
  120.    if (echo__d == 0) return FALSE;
  121.  
  122.    echo__filetype   = filetype;
  123.    echo__saveproc   = saveproc;
  124.    echo__sendproc   = sendproc;
  125.    echo__printproc  = printproc;
  126.    echo__savehandle = handle;
  127.    echo__estsize    = estsize;
  128.  
  129.    dbox_show(echo__d);
  130.    xfersend_close_on_xfer(TRUE, dbox_syshandle(echo__d));
  131.  
  132.    echo__setname((wimp_w) dbox_syshandle(echo__d), echo__FIcon, spr);
  133.  
  134.    dbox_raw_eventhandler(echo__d, echo__saveevents, echo__savehandle);
  135.    dbox_setfield(echo__d, echo__FName, name);
  136.    strncpy(echo__filename, name, 256);
  137.  
  138.    while (dbox_fillin(echo__d) == echo__FOK)
  139.    {
  140.       int i;
  141.       BOOL dot;
  142.  
  143.       dbox_getfield(echo__d, echo__FName, echo__filename, 256);
  144.  
  145.       for (i = 0, dot = FALSE; !dot && echo__filename[i]; )
  146.          dot = echo__filename[i++] == '.';
  147.  
  148.       if (!dot)
  149.       {  werr(FALSE, msgs_lookup("saveas1:To save, drag the icon to a directory display."));
  150.          continue;
  151.       }
  152.  
  153.       xfersend_set_fileissafe(TRUE);
  154.  
  155.       if (echo__saveproc(echo__filename, echo__savehandle) == TRUE && !dbox_persist())
  156.          break;
  157.    }
  158.  
  159.    xfersend_close_on_xfer(FALSE, 0);
  160.    xfersend_clear_unknowns();
  161.    dbox_hide(echo__d);
  162.    dbox_dispose(&echo__d);
  163.    echo__d = 0;
  164.  
  165.    return TRUE;
  166. }
  167.  
  168. /* If the specified file exists in the app copy it from
  169.  * <echo$dir>.link.name substituting the application
  170.  * name into it
  171.  */
  172.  
  173. static BOOL echo__copy(char *name, char *app, char *als, BOOL cond)
  174. {  os_filestr fcb;
  175.    char in[256], out[256];
  176.    char path[256];
  177.    FILE *inf, *outf;
  178.    int c;
  179.    char *lp, *np;
  180.  
  181.    strcpy(path, app);
  182.  
  183.    for (lp = np = path; *np; np++)
  184.       if (*np == '.' || *np == ':')
  185.          lp = np;
  186.  
  187.    if (lp != path) *lp = '\0';
  188.  
  189.    if (cond)
  190.    {  sprintf(in, "%s.%s", app, name);
  191.    
  192.       fcb.action = 17;
  193.       fcb.name = in;
  194.    
  195.       if (wimpt_complain(os_file(&fcb)))
  196.          return FALSE;
  197.    
  198.       if (fcb.action == 0)
  199.          return TRUE;   /* OK, just don't copy it */
  200.    }
  201.  
  202.    sprintf(in, "<Echo$Dir>.Link.%s", name);
  203.  
  204.    if (wimpt_complain(os_file(&fcb)))
  205.       return FALSE;
  206.  
  207.    if (fcb.action == 0)
  208.       return TRUE;   /* OK, just don't copy it */
  209.  
  210.    sprintf(out, "%s.%s", als, name); 
  211.  
  212.    if (inf = fopen(in, "r"), !inf)
  213.       goto fail;
  214.  
  215.    if (outf = fopen(out, "w"), !outf)
  216.       goto fail1;
  217.  
  218.    c = getc(inf);
  219.    while (c != EOF)
  220.    {  if (c == '?')
  221.          fprintf(outf, "%s", app);
  222.       else if (c == '@')
  223.          fprintf(outf, "%s", path);
  224.       else
  225.          putc(c, outf);
  226.       c = getc(inf);
  227.    }
  228.  
  229.    fclose(inf);
  230.    fclose(outf);
  231.  
  232.    fcb.action = 1;
  233.    fcb.name = out;
  234.    
  235.    if (wimpt_complain(os_file(&fcb))) return FALSE;
  236.  
  237.    return TRUE;   
  238.  
  239. fail1:
  240.    fclose(inf);
  241. fail:
  242.    werr(FALSE, msgs_lookup("main7"), name);
  243.    return FALSE;
  244. }
  245.  
  246. static BOOL echo__saver(char *name, void *handle)
  247. {  os_filestr fcb;
  248.  
  249.    fcb.action = 17;
  250.    fcb.name = name;
  251.  
  252.    if (!os_file(&fcb) && fcb.action != 0)
  253.    {  werr(FALSE, msgs_lookup("main6"), name);
  254.       return FALSE;
  255.    }
  256.  
  257.    fcb.action = 8;
  258.    fcb.name = name;
  259.  
  260.    if (wimpt_complain(os_file(&fcb))) return FALSE;
  261.  
  262.    if (!echo__copy("!Boot",     echo__srcname, name, TRUE) ||
  263.        !echo__copy("!Run",      echo__srcname, name, TRUE) ||
  264.        !echo__copy("!Original", echo__srcname, name, FALSE))
  265.       return FALSE;
  266.  
  267.    return TRUE;
  268. }
  269.  
  270. static BOOL echo__sender(void *handle, int *maxbuf)
  271. {
  272.    handle = handle;
  273.    maxbuf = maxbuf;
  274.  
  275.    werr(FALSE, msgs_lookup("main4"));
  276.    return FALSE;
  277. }
  278.  
  279. static int echo__printer(char *name, void *handle)
  280. {
  281.    name = name;
  282.    handle = handle;
  283.  
  284.    werr(FALSE, msgs_lookup("main4"));
  285.    return xfersend_printFailed;
  286. }
  287.  
  288. static void echo__process(char *name, int type)
  289. {  char *lp, *np;
  290.  
  291.    if (type != 0x2000)
  292.    {  werr(FALSE, msgs_lookup("main3"));
  293.       return;
  294.    }
  295.  
  296.    strcpy(echo__srcname, name);
  297.  
  298.    for (np = name, lp = name; *np; np++)
  299.       if (*np == '.' || *np == ':')
  300.          lp = np + 1;
  301.  
  302.    echo__saveas(0x2000, lp, 1,
  303.                 echo__saver,
  304.                 echo__sender,
  305.                 echo__printer,
  306.                 NULL, lp);
  307. }
  308.  
  309. static void echo__iconproc(wimp_eventstr *e, void *handle)
  310. {
  311.    switch (e->e)
  312.    {  case wimp_ESEND:
  313.       case wimp_ESENDWANTACK:
  314.          switch (e->data.msg.hdr.action)
  315.          {  case wimp_MDATALOAD:
  316.                echo__process(e->data.msg.data.dataload.name,
  317.                              e->data.msg.data.dataload.type);
  318.                break;
  319.          }
  320.          break;
  321.    }
  322. }
  323.  
  324. int main()
  325. {
  326.    wimpt_init("Echo");
  327.    res_init("Echo");
  328.    msgs_init();
  329.    template_init();
  330.  
  331.    baricon("!echo", 1, echo__click);
  332.  
  333.    win_register_event_handler(win_ICONBARLOAD, echo__iconproc, NULL);
  334.  
  335.    event_attachmenu(win_ICONBAR,
  336.                     menu_new("Echo", msgs_lookup("main1:>Info,Quit")),
  337.                     echo__menuhandler, NULL);
  338.  
  339.    for (;;)
  340.       event_process();
  341.  
  342.    return 0;
  343. }
  344.